package tetris;

import java.util.Random;
import java.lang.Math;

public class Form {

    enum Piece { None, PieceZ, PieceS, PieceLine, PieceT, PieceSquare, PieceL, PieceMirrored };
    
    private final int NUM_OF_PIECES = 7;
    private Piece pieceShape;
    private int location[][];
    private int[][][] locationTab;

    /**
     * Creates tetris piece and sets default spawn coordinates.
     */
    public Form() {
    	
        location = new int[4][2];
        generateShape(Piece.None);
    }

    /**
     * Sets appropriate shape for piece.
     * @param shape sets shape
     */
    public void generateShape(Piece shape) {

         locationTab = new int[][][] {
            { { 0, 0 },   { 0, 0 },   { 0, 0 },   { 0, 0 } },	// None
            { { 0, -1 },  { 0, 0 },   { -1, 0 },  { -1, 1 } },	// PieceZ
            { { 0, -1 },  { 0, 0 },   { 1, 0 },   { 1, 1 } },	// PieceS
            { { 0, -1 },  { 0, 0 },   { 0, 1 },   { 0, 2 } },	// PieceLine
            { { -1, 0 },  { 0, 0 },   { 1, 0 },   { 0, 1 } },	// PieceT
            { { 0, 0 },   { 1, 0 },   { 0, 1 },   { 1, 1 } },	// PieceSquare
            { { -1, -1 }, { 0, -1 },  { 0, 0 },   { 0, 1 } },	// PieceL
            { { 1, -1 },  { 0, -1 },  { 0, 0 },   { 0, 1 } }	// PieceMirrored
        };

        for (int i = 0; i < 4 ; i++) {
            for (int j = 0; j < 2; ++j) {
                location[i][j] = locationTab[ shape.ordinal() ][ i ][ j ];
            }
        }
        
        pieceShape = shape;
    }

    private void setX( int temp, int x ) {
    	location[temp][0] = x;
    }
    
    private void setY( int temp, int y ) { 
    	location[temp][1] = y; 
    }
    
    public int x( int temp ) { 
    	return location[temp][0]; 
    }
    
    public int y( int temp ) { 
    	return location[temp][1]; 
    }
    
    public Piece getShape()  { 
    	return pieceShape; 
    		}

    /**
     * Gets random tetris piece using Random class and nextInt() method.
     */
    public void generateRandomPiece() {
    	
        Random r = new Random();
        int x = Math.abs( r.nextInt() ) % NUM_OF_PIECES + 1;
        Piece[] values = Piece.values(); 
        generateShape( values[x] );
    }

    public int minX() {
    	
      int m = location[0][0];
      for (int i=0; i < 4; i++) {
          m = Math.min(m, location[i][0]);
      }
      return m;
    }


    public int minY() {
    	
      int m = location[0][1];
      for (int i=0; i < 4; i++) {
          m = Math.min(m, location[i][1]);
      }
      return m;
    }

    /**
     * Allows to rotate tetris piece in clockwise way.
     * @return returns coordinates of rotated piece.
     */
    public Form pieceRotate() {
    	
        if (pieceShape == Piece.PieceSquare)
            return this;

        Form result = new Form();
        result.pieceShape = pieceShape;

        for (int i = 0; i < 4; ++i) {
            result.setX(i, -y( i ) );
            result.setY(i, x( i ) );
        }
        return result;
    }
}
